package com.hd.view.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.hd.service.MemberManager;
import com.hd.service.PurchaseManager;
import com.hd.service.RepairManager;
import com.hd.utils.RequestUtils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Action 基类
 * @author jiaoyang
 *
 */
public class BaseAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	protected MemberManager memberManager;
	
	protected PurchaseManager purchaseManager;
	
	protected RepairManager repairManager;
	
	//提示信息
	protected String message;
	//状态
	protected boolean statusTemp;
	//系统当前时间
	protected long systemDate = System.currentTimeMillis();
	/**
	 * 分页使用的三个参数： currentPage为当前要显示的页，默认为第一页
	 * totalPage需要设置为DataPage.getTotalPageCount()方法返回的值
	 * pageSize为每页需要显示的记录数，默认为5
	 */
	protected String currentPage = "1";
	protected String totalPage = "0";
	protected int pageSize = 5;
	
	
	/**
	 * 获取Request对象
	 * 
	 * @return current request
	 */
	protected HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	
	/**
	 * 获取Response对象
	 * 
	 * @return current response
	 */
	protected HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
	}
	
	/**
	 * 获取HTTP请求头中的所有cookie
	 * 
	 * return cookies
	 */
	protected Cookie[] getCookies(){
		return getRequest().getCookies();
	}
	
	/**
	 * 获取HTTP请求头中的某个cookie
	 * 
	 * return Cookie
	 */
	protected Cookie getCookie(String cookieName){
		return RequestUtils.getCookie(getRequest(), cookieName);
	}
	
	/**
	 * 根据cookie的key值移除cookie
	 */
	protected void removeCoookie(String cookieName){
		if(!StringUtils.isEmpty(getCookie(cookieName).getValue())){
			RequestUtils.removeCookie(getResponse(), cookieName);
		}
	}
	
	/**
	 * 从Cookie中获取登录名
	 */
	public String getLoginName(){
		if(getCookie("loginName")!=null){
			return getCookie("loginName").getValue();
		}else{
			return null;
		}
	}
	
	/**
	 * 从Cookie中获取密码
	 */
	public String getPassword(){
		if(getCookie("password") != null){
			return getCookie("password").getValue();
		}else{
			return null;
		}
	}

	
	
/*===============get,set方法=============================================*/	
	
	
	public int getCurrentPage() {
		return Integer.parseInt(currentPage);
	}

	public MemberManager getMemberManager() {
		return memberManager;
	}

	public void setMemberManager(MemberManager memberManager) {
		this.memberManager = memberManager;
	}

	public PurchaseManager getPurchaseManager() {
		return purchaseManager;
	}

	public void setPurchaseManager(PurchaseManager purchaseManager) {
		this.purchaseManager = purchaseManager;
	}

	public RepairManager getRepairManager() {
		return repairManager;
	}

	public void setRepairManager(RepairManager repairManager) {
		this.repairManager = repairManager;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage + "";
	}

	public int getTotalPage() {
		return Integer.parseInt(totalPage);
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage + "";
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean getStatusTemp() {
		return statusTemp;
	}

	public void setStatusTemp(boolean statusTemp) {
		this.statusTemp = statusTemp;
	}

	public long getSystemDate() {
		return systemDate;
	}

	public void setSystemDate(long systemDate) {
		this.systemDate = systemDate;
	}
}
